package com.crm.system.controller.InternalTrainingManager;

import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.internalTrainingManager.NxContact;
import com.crm.model.entity.internalTrainingManager.NxFollow;
import com.crm.model.qo.customerManager.CustomerAddQO;
import com.crm.model.qo.customerManager.CustomerQO;
import com.crm.model.qo.internalTrainingManager.NxContactQO;
import com.crm.model.qo.internalTrainingManager.NxFollowQO;
import com.crm.model.vo.customerManager.CustomerVO;
import com.crm.model.vo.internalTrainingManager.NxFollowVO;
import com.crm.system.service.customerManager.CustomerService;
import com.crm.system.service.internalTrainingManager.CustomerUserService;
import com.crm.system.service.internalTrainingManager.NxContactService;
import com.crm.system.service.internalTrainingManager.NxFollowService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/internalTrainingManager")
@Slf4j
public class InternalTrainingManagerContorller {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerUserService customerUserService;

    @Autowired
    private NxContactService nxContactService;

    @Autowired
    private NxFollowService nxFollowService;

    /**
     * 内训部客户列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param customerQO
     * @return
     */
    @PostMapping("/customerList")
    public DataResponse selectCustomerVOListForInT(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                   @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                   @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                                   CustomerQO customerQO//查询条件
    ) {
        try {
            DhPageInfo<CustomerVO> customerVOList = customerService.selectCustomerVOListForInT(pageIndex, pageSize, customerQO, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), customerVOList);
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller selectCustomerVOListForInT error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 内训部-领取客户
     * @param customerId
     * @return
     */
    @PostMapping("receiveCustomer")
    public DataResponse receiveCustomer(@RequestParam(value = "customerId", required = false) String customerId) {
        try {
            if (!StringUtils.isBlank(customerId)) {
                String message = customerUserService.receiveCustomerForInT(customerId,null);
                return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), message);
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller receiveCustomer error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 内训部-释放客户
     * @param customerId
     * @return
     */
    @PostMapping("releaseCustomer")
    public DataResponse releaseCustomer(@RequestParam(value = "customerId", required = false) String customerId) {
        try {
            if (!StringUtils.isBlank(customerId)) {
                return customerUserService.releaseCustomerForInT(customerId) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
            }
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller releaseCustomer error:", e);
            return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
        }
    }

    /**
     * 我的客户
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param customerQO
     * @return
     */
    @PostMapping("MyCustomer")
    public DataResponse selectCustomerVOListByOwner(HttpServletRequest request,
                                                   @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                   @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                   @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                   @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                                   CustomerQO customerQO
    ) {
        try {
            DhPageInfo<CustomerVO> customerVOList = customerService.selectCustomerVOListByOwner(pageIndex, pageSize, customerQO, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), customerVOList);
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller MyCustomer error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 内训联系人列表
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param nxContactQO
     * @return
     */
    @PostMapping("contactList")
    public DataResponse contactList(HttpServletRequest request,
                                    @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                    @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                    NxContactQO nxContactQO
    ) {
        try {
            DhPageInfo<NxContact> contactVOList = nxContactService.selectContactVOListByCustomerId(pageIndex, pageSize, nxContactQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), contactVOList);
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller contactList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 新增内训联系人
     * @param nxContact
     * @return
     */
    @PostMapping("insertNxContact")
    public DataResponse insertNxContact(NxContact nxContact) {
        return nxContactService.insertNxContact(nxContact) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 内训跟进
     * @param nxFollow
     * @return
     */
    @PostMapping("/follow")
    public DataResponse insertNxFollow(NxFollow nxFollow) {
        return nxFollowService.insertNxFollow(nxFollow) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 获取联系人下拉
     * @return
     */
    @PostMapping("/getNxContactByOwner")
    public DataResponse getNxContactByOwner(NxContactQO nxContactQO){
        try {
            List<NxContact> nxContactByOwner = nxContactService.getNxContactByOwner(nxContactQO);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), nxContactByOwner);
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller getNxContactByOwner error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }

    /**
     * 新增客户
     * @param request
     * @param customerAddQO
     * @return
     */
    @PostMapping("/addCustomer")
    public Map<String, String> addCustomer(HttpServletRequest request, @RequestBody CustomerAddQO customerAddQO) {
        Map<String, String> map = new HashMap<String, String>();
        try {
            map = customerService.addCustomerinT(customerAddQO);
            return map;
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller addCustomer error:", e);
            //return new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
            map.put("code", "900000");
            map.put("customerId", "");
            return map;
        }
    }


    /**
     * 跟进记录
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param nxFollowQO
     * @return
     */
    @PostMapping("/selectFollowRecordList")
    public DataResponse selectFollowRecordList(HttpServletRequest request,
                                                    @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                    @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                    @RequestParam(value = "sortName", required = false) String sortName,//排序列名字
                                                    @RequestParam(value = "sortOrder", required = false) String sortOrder,//排序规则(asc/desc)
                                                    NxFollowQO nxFollowQO
    ) {
        try {
            DhPageInfo<NxFollowVO> customerVOList = nxFollowService.selectFollowRecordList(pageIndex, pageSize, nxFollowQO, sortName, sortOrder);
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode(), customerVOList);
        } catch (Exception e) {
            log.error("InternalTrainingManagerContorller selectFollowRecordList error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }


    /**
     * 线上跟进插入内训跟进记录表
     * @param nxFollowQO
     * @return
     */
    @PostMapping("/onlineFollow")
    public DataResponse onlineFollow(@RequestBody NxFollowQO nxFollowQO) {
        return nxFollowService.onlineFollow(nxFollowQO) ? new DhResponse(ResponseCodeEnum.OK.getResponseCode()) : new DhResponse(ResponseCodeEnum.SERVICE_ERROR.getResponseCode());
    }

    /**
     * 查询是否存在联系人
     * @param contactInformation 联系电话
     */
    @PostMapping("/selectNxContactByContactInformation")
    public DataResponse selectNxContactByContactInformation(@RequestParam String contactInformation,@RequestParam String customerId) {
        try {
            String name = nxContactService.selectNxContactByContactInformation(contactInformation,customerId);

            return StringUtils.isEmpty(name) ? new DhResponse(ResponseCodeEnum.ERROR.getResponseCode()) : new DhResponse(ResponseCodeEnum.OK.getResponseCode(),name);
        } catch (Exception e) {
            log.error("internalTrainingManager selectNxContactByContactInformation error:", e);
            return new DhResponse(ResponseCodeEnum.ERROR.getResponseCode());
        }
    }
}